package yyds;

//环形队列
public class CircleQueue {
    private int maxSize;
    private int head;
    private int tail;
    private int [] arr;

    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }
//    判断队列是否已满
    public boolean isFull(){
        return (tail+1)%maxSize == head;
    }
//    判断队列是否为空
    public boolean isEmpty(){
        return tail==head;
    }
//    添加元素到队列
    public void addToQueue(int a){
        if (isFull()){
            System.out.println("队列已满");
            return;
        }
        arr[tail] = a;
        tail = (tail+1)%maxSize;
    }
//    取出数据
    public int getFromQueue(){
        if (isEmpty()){
//            throw new RuntimeException("队列为空，不能取数据");
            System.out.println("队列为空");
            return -1;
        }
        int i = arr[head];
        head = (head+1)%maxSize;

        return i;
    }

//    获取队列有效值
    public int getSize(){
        return (tail+maxSize-head)%maxSize;
    }

//    显示队列的所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        for (int i = head; i <head+getSize() ; i++) {
            System.out.println(arr[i%maxSize]);
        }
    }

 //    显示队列头
    public void oneQueue(){
        if (isEmpty()){
            System.out.println("队列为空");
            return;
        }
        System.out.println(arr[head+1]);
    }
}
